﻿Imports MindFusion.Scheduling
Imports MindFusion.Scheduling.WinForms

Public Class frmAgenda
    Private szSQL As String
    Private rs As ADODB.Recordset
    Private currentDate As Date

    Private Sub frmAgenda_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.Text = "AGENDA - " & gNomAgence & " - " & gUserNom
        ' initialiser agenda semaine
        currentDate = Format(Now, "yyyy/MM/dd")
        CAL_AGENDA.CurrentView = CalendarView.Timetable
        CAL_AGENDA.GroupType = GroupType.None
        CAL_AGENDA.TimetableSettings.Dates.Clear()
        CAL_AGENDA.Date = getLundiPrecedent(currentDate)
        For i = 0 To 5
            CAL_AGENDA.TimetableSettings.Dates.Add(CAL_AGENDA.Date.AddDays(i))
        Next i
        CAL_AGENDA.EndDate = CAL_AGENDA.Date.AddDays(5)
        CAL_AGENDA.CurrentTime = currentDate
        LoadPlanning()
        ' positionner le planning sur 6h
        CAL_AGENDA.VScrollPos = 16

    End Sub

    Sub LoadPlanning()

        Dim app As Appointment

        ' effacer la sélection
        CAL_AGENDA.Selection.Reset()
        CAL_AGENDA.ResetDrag()
        CAL_AGENDA.BeginInit()
        CAL_AGENDA.Schedule.Clear()
        ' Lire la journée du planning
        szSQL = "CALL spGET_PROSPECTS_RDV_byDATE(" & gUserAgence & ",'" & Format(CAL_AGENDA.Date, "yyyy/MM/dd") & "','" & _
                Format(CAL_AGENDA.EndDate, "yyyy/MM/dd") & "')"
        GetRecordset(ctDSN_AEM, szSQL, rs)

        Do While Not rs.EOF
            app = New Appointment()
            Dim debut As DateTime = rs.Fields("debut_rdv").Value
            Dim fin As DateTime = rs.Fields("fin_rdv").Value
            app.StartTime = New Date(debut.Year, debut.Month, debut.Day, debut.Hour, debut.Minute, 0)
            app.EndTime = New Date(fin.Year, fin.Month, fin.Day, fin.Hour, fin.Minute, 0)
            app.HeaderText = nv(rs.Fields("objet_rdv").Value)
            app.Tag = rs.Fields("cd_prospect").Value
            app.Id = rs.Fields("no_ligne").Value

                If app.Tag.ToString.Length = 6 Then
                    If rs.Fields("relance").Value = False Then
                        ' rdv avec un prospect = jaune pale
                        app.Style.Brush = New MindFusion.Drawing.SolidBrush(Color.AntiqueWhite)
                    Else
                        ' relance de prospect = rose pale
                        app.Style.Brush = New MindFusion.Drawing.SolidBrush(Color.LightPink)
                    End If
                Else
                    ' rdv autre = bleu pale
                    app.Style.Brush = New MindFusion.Drawing.SolidBrush(Color.LightBlue)
                End If
                app.AllowChangeEnd = True
                app.AllowChangeStart = True
                app.AllowMove = True
                app.AllDayEvent = False
                CAL_AGENDA.Schedule.Items.Add(app)
                rs.MoveNext()
        Loop
        ' Close the recordset AND connection.
        rs.Close()
        rs = Nothing

        CAL_AGENDA.EndInit()

    End Sub

    Private Sub CAL_AGENDA_DateClick(ByVal sender As Object, ByVal e As MindFusion.Scheduling.WinForms.ResourceDateEventArgs) Handles CAL_AGENDA.DateClick
        ' double-click ? 
        If e.Clicks = 2 And e.Button = System.Windows.Forms.MouseButtons.Left Then
            ' vue journalier ?
            If CAL_AGENDA.CurrentView = CalendarView.Timetable Then
                ' insérer nouveau rdv
                If IsUserAuthorised(2) = False Then
                    Exit Sub
                End If
                '' si date de rdv passée, interdire modif
                'If DateDiff(DateInterval.Day, e.Date, Now) > 0 Then
                '    MsgBox("Cette journée est déjà passée. Modification interdite.", MsgBoxStyle.Information)
                '    Exit Sub
                'End If
                ' afficher la ligne pour creation
                frmAgendaLigne.pNoLigne = 0
                frmAgendaLigne.pDateTime = e.Date
                frmAgendaLigne.ShowDialog()
                ' réaffiche la viewlist
                LoadPlanning()
            Else
                ' vue mois ?
                CAL_AGENDA.TimetableSettings.Dates.Clear()
                CAL_AGENDA.TimetableSettings.Dates.Add(e.Date)
                CAL_AGENDA.Date = e.Date
                ' affichier la vue journée
                CAL_AGENDA.CurrentView = CalendarView.Timetable
            End If

        End If


    End Sub

    Private Sub CAL_AGENDA_Draw(ByVal sender As Object, ByVal e As MindFusion.Scheduling.WinForms.DrawEventArgs) Handles CAL_AGENDA.Draw
        Dim bounds As Rectangle = e.Bounds

        If CAL_AGENDA.CurrentView = CalendarView.Timetable Then
            bounds.Inflate(-1, -1)
            ' colorier les cellules selon les horaires : travail = jaune pale, non travail = fumée blanc
            If (e.StartTime.Hours < CAL_AGENDA.TimetableSettings.WorkTimeStartHour Or _
                      e.EndTime.Hours > CAL_AGENDA.TimetableSettings.WorkTimeEndHour) Then
                e.Graphics.FillRectangle(Brushes.WhiteSmoke, bounds)
            Else
                e.Graphics.FillRectangle(Brushes.LightYellow, bounds)
            End If
        End If

    End Sub

    Private Sub CAL_AGENDA_ItemClick(ByVal sender As Object, ByVal e As MindFusion.Scheduling.WinForms.ItemMouseEventArgs) Handles CAL_AGENDA.ItemClick
        If System.Windows.Forms.MouseButtons.Left Then
            ' Get the clicked event
            Dim app As Appointment = e.Item 'as Appointment

            ' si date de rdv passée, interdire modif
            'If DateDiff(DateInterval.Day, app.StartTime, Now) > 0 Then
            '    Exit Sub
            'End If
            ' double-click = Modifier
            If e.Clicks = 2 Then
                ' afficher les détails de la ligne NoLigne
                frmAgendaLigne.pNoLigne = app.Id
                frmAgendaLigne.pDateTime = app.StartTime.Date
                frmAgendaLigne.ShowDialog()
                ' réaffiche la viewlist
                LoadPlanning()
            End If
        End If
    End Sub

    Private Sub CAL_AGENDA_ItemDeleting(ByVal sender As Object, ByVal e As MindFusion.Scheduling.WinForms.ItemConfirmEventArgs) Handles CAL_AGENDA.ItemDeleting
        ' interdire suppression 
        e.Confirm = False

    End Sub

    Private Sub CAL_AGENDA_ItemModified(ByVal sender As Object, ByVal e As MindFusion.Scheduling.WinForms.ItemModifiedEventArgs) Handles CAL_AGENDA.ItemModified

        Dim app As Appointment = e.Item 'as Appointment

        ' mettre à jpur le rendez-vous suite à un move ou resize 
        szSQL = "UPDATE prospects_rdv SET debut_rdv='" & Format(app.StartTime, "yyyy/MM/dd hh:mm") & "', fin_rdv='" & Format(app.EndTime, "yyyy/MM/dd hh:mm") & "', cd_uti='" & gUserCode & _
                "' WHERE no_ligne=" & app.Id
        ExecQuery(ctDSN_AEM, szSQL)

    End Sub



    Private Sub CAL_AGENDA_VisibleDateChanged(ByVal sender As Object, ByVal e As MindFusion.Scheduling.WinForms.DateChangedEventArgs) Handles CAL_AGENDA.VisibleDateChanged
        ' changement de mois
        currentDate = New Date(e.NewDate.Year, e.NewDate.Month, 1)
        LoadPlanning()
    End Sub

    ' Tester la connection avec la base de données
    Private Function IsConnectionOK() As Boolean

        Dim conn As ADODB.Connection

        'enable error handler
        On Error GoTo ErrorHandler

        IsConnectionOK = False
        conn = New ADODB.Connection

        ' tester la connection avec MySQL
        conn.Open(ctDSN_AEM)
        conn.Close()
        ' conn.Dispose()
        IsConnectionOK = True
        Exit Function

ErrorHandler:
        MsgBox("Erreur lors de la connection à la BD : " & Err.Number & vbCrLf & Err.Description, MsgBoxStyle.Critical)
        End

    End Function


    Private Sub BO_MONTH_Click(sender As System.Object, e As System.EventArgs) Handles BO_MONTH.Click
        ' initialiser agenda mois
        currentDate = Format(Now, "yyyy/MM/dd")
        CAL_AGENDA.CurrentView = CalendarView.SingleMonth
        CAL_AGENDA.GroupType = GroupType.None
        CAL_AGENDA.TimetableSettings.Dates.Clear()
        CAL_AGENDA.Date = New Date(currentDate.Year, currentDate.Month, 1)
        CAL_AGENDA.EndDate = CAL_AGENDA.Date.AddDays(5)
        CAL_AGENDA.CurrentTime = currentDate
        LoadPlanning()
    End Sub

    Private Sub BO_WEEK_Click(sender As System.Object, e As System.EventArgs) Handles BO_WEEK.Click
        ' initialiser agenda semaine
        currentDate = Format(Now, "yyyy/MM/dd")
        CAL_AGENDA.CurrentView = CalendarView.Timetable
        CAL_AGENDA.GroupType = GroupType.None
        CAL_AGENDA.TimetableSettings.Dates.Clear()
        CAL_AGENDA.Date = getLundiPrecedent(currentDate)
        For i = 0 To 5
            CAL_AGENDA.TimetableSettings.Dates.Add(CAL_AGENDA.Date.AddDays(i))
        Next i
        CAL_AGENDA.EndDate = CAL_AGENDA.Date.AddDays(5)
        CAL_AGENDA.CurrentTime = currentDate
        LoadPlanning()
    End Sub

    Private Sub BO_PREVIOUS_Click(sender As System.Object, e As System.EventArgs) Handles BO_PREVIOUS.Click
        If CAL_AGENDA.CurrentView = CalendarView.Timetable Then
            CAL_AGENDA.GroupType = GroupType.None
            CAL_AGENDA.TimetableSettings.Dates.Clear()
            CAL_AGENDA.Date = CAL_AGENDA.Date.AddDays(-7)
            For i = 0 To 5
                CAL_AGENDA.TimetableSettings.Dates.Add(CAL_AGENDA.Date.AddDays(i))
            Next i
            CAL_AGENDA.EndDate = CAL_AGENDA.Date.AddDays(5)
            CAL_AGENDA.CurrentTime = CAL_AGENDA.Date
            LoadPlanning()
        End If
    End Sub

    Private Sub BO_NEXT_Click(sender As System.Object, e As System.EventArgs) Handles BO_NEXT.Click
        If CAL_AGENDA.CurrentView = CalendarView.Timetable Then
            CAL_AGENDA.GroupType = GroupType.None
            CAL_AGENDA.TimetableSettings.Dates.Clear()
            CAL_AGENDA.Date = CAL_AGENDA.Date.AddDays(7)
            For i = 0 To 5
                CAL_AGENDA.TimetableSettings.Dates.Add(CAL_AGENDA.Date.AddDays(i))
            Next i
            CAL_AGENDA.EndDate = CAL_AGENDA.Date.AddDays(5)
            CAL_AGENDA.CurrentTime = CAL_AGENDA.Date
            LoadPlanning()
        End If
    End Sub

End Class